package com.cs.peanut.player;

import android.app.AlertDialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.cs.peanut.base.util.TLog;
import com.cs.peanut.player.api.Api;

import java.io.File;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by peanut on 2018/06/11.
 */

public class TransferDialog extends AlertDialog {
    protected TransferDialog(@NonNull Context context) {
        super(context);
    }

    protected TransferDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
    }

    protected TransferDialog(@NonNull Context context, boolean cancelable, @Nullable
            OnCancelListener cancelListener) {
        super(context, cancelable, cancelListener);
    }

    interface OnFisish {
        void finish();
    }

    static class TransferBuild extends Builder {
        ProgressBar mProgressBar;
        OnFisish mOnFisish;
        TextView mTextView;

        public TransferBuild(@NonNull Context context) {
            super(context);
        }

        public TransferBuild(@NonNull Context context, int themeResId) {
            super(context, themeResId);
        }

        public TransferBuild(Context context, String path, String ip, OnFisish onFisish) {
            super(context);
            mOnFisish = onFisish;
            setTitle("提示：");
            setMessage("文件传输中...");
            View view = LayoutInflater.from(context).inflate(R.layout.item_myprogressbar, null);
            mProgressBar = new ProgressBar(context, null, android.R.attr
                    .progressBarStyleHorizontal);
            mProgressBar.setMax(100);
            setView(view);
            mProgressBar = view.findViewById(R.id.progress);
            mTextView = view.findViewById(R.id.textView);
            transferToServer(path, ip);
        }

        private void transferToServer(String url, String ip) {
            File file = new File(url);
            RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"),
                    file);
            RequestBodyProgress requestFile = new RequestBodyProgress(requestBody, (progress, total, done) -> {
                if (done) {
                    mProgressBar.setProgress(100);
                    mTextView.setText("100%");
                    mOnFisish.finish();
                } else {
                    float a = (float) progress/total;
                    mProgressBar.setProgress(Math.round(a*100));
                    mTextView.setText(mProgressBar.getProgress()+"%");
                    if(mProgressBar.getProgress() == 100){
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            });
            MultipartBody.Part body = MultipartBody.Part.createFormData("picture", file.getName()
                    , requestFile);
            String descriptionString = "hello, this is description speaking";
            RequestBody description = RequestBody.create(MediaType.parse("multipart/form-data"),
                    descriptionString);
            Call<ResponseBody> call = Api.get(ip).upload(description, body);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {

                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    t.printStackTrace();
                    TLog.e(t.getMessage());
                }
            });
        }
    }
}
